
import {
  getEventMaintenanceApi,
  addEventMaintenanceApi,
  delEventMaintenanceApi,
  standScoreUpdateApi,
  getStandScoreListApi,
  uploadApi,
  updateApi,
  uploadEventMaintenanceApi,
  removeFileApi
} from '@/services/eventCodeMaintenance.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    eventMaintenanceLoading: false,
    eventMaintenanceList: [],
    addEventMaintenanceLoading: false,
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    },
    standScoreList: [],
    getStandScoreListLoading: false,
    uploadLoading: false,
    upload2Loading: false,
    upload2Data: [],
    removeFileLoading: false
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取列表
    async getEventMaintenance({ commit, state, dispatch }, params) {
      commit('setState', { key: 'eventMaintenanceLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getEventMaintenanceApi(apiParams);
      dispatch('setting/setAcarsTime', {}, { root: true });
      /* eslint-disable */
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total } });
      commit('setState', { key: 'eventMaintenanceList', payload: rep.data.list });
      commit('setState', { key: 'eventMaintenanceLoading', payload: false });
    },
    // 新增
    async addEventMaintenance({ commit }, params) {
      commit('setState', { key: 'addEventMaintenanceLoading', payload: true });
      try {
        const rep = await addEventMaintenanceApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addEventMaintenanceLoading', payload: false });
      }
    },
    // 删除
    async delEventMaintenance({ commit }, params) {
      try {
        await delEventMaintenanceApi(params);
        // if (rep.data === '成功') {
        message.success('操作成功');
        return Promise.resolve(true);
        // } else {
        //   message.error(rep.message);
        // }
      } catch (error) {
        message.error('操作失败');
      }
    },

    // 上传
    async uploadFile({ commit }, params) {
      commit('setState', { key: 'uploadLoading', payload: true });
      try {
        const rep = await uploadApi(params);
        if (rep.code === 200) {
          message.success('操作成功');
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'uploadLoading', payload: false });
      }
    },
    // 上传2
    async uploadEventMaintenance({ commit, state }, params) {
      commit('setState', { key: 'upload2Loading', payload: true });
      try {
        const rep = await uploadEventMaintenanceApi(params);

        if (rep.code === 200) {
          commit('setState', { key: 'upload2Data', payload: [...state.upload2Data, ...rep.data] });
          // message.success('操作成功');
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'upload2Loading', payload: false });
      }
    },
    // 获取全局
    async getStandScoreList({ commit, state }, params) {
      commit('setState', { key: 'getStandScoreListLoading', payload: true });
      let rep = await getStandScoreListApi(params);
      rep = rep.data.filter(item => item.too_level !== 'null').sort((a, b) => {
        return a.score - b.score;
      });
      commit('setState', { key: 'standScoreList', payload: rep });
      commit('setState', { key: 'getStandScoreListLoading', payload: false });
    },
    async removeFile({ commit }, params) {
      commit('setState', { key: 'removeFileLoading', payload: true });
      try {
        const rep = await removeFileApi(params);
        if (rep.code === 200) {
          message.success('操作成功');
          return Promise.resolve(true);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'removeFileLoading', payload: false });
      }
    },
    // 修改全局
    async standScoreUpdate({ commit }, params) {
      commit('setState', { key: 'getStandScoreListLoading', payload: true });
      try {
        const rep = await standScoreUpdateApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'getStandScoreListLoading', payload: false });
      }
    },
    async updateEventCode({ commit }, params) {
      commit('setState', { key: 'addEventMaintenanceLoading', payload: true });
      try {
        const rep = await updateApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addEventMaintenanceLoading', payload: false });
      }
    }
  }
};
